//
//  MM_TitleView.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/9/11.
//

#import "MM_TitleView.h"

@implementation MM_TitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setTitleFrame];
    }
    return self;
}

// 初始化子控件布局
-(void)setTitleFrame {
    
    self.bounds = CGRectMake(0, 0, ScreenW, NaviBarHeight);
    self.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
    self.title = @"";
    [self addTitleSubview];
    
}
// 增加子控件
- (void)addTitleSubview {
    
    // 添加返回按钮
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *backImg = [UIImage imageNamed:@"orderComeBack"];
    [back setImage:backImg forState:UIControlStateNormal];
    [back addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:back];
    back.titleLabel.font = FONT(RATIO(28));
    self.backBtn = back;
    
    // 添加标题
    UILabel *title = [[UILabel alloc] init];
    title.text = self.title;
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [ColorUtil colorWithHexadecimal:@"#333333"];
    title.font = FONT(RATIO(34));
    [self addSubview:title];
    self.titleLabel = title;
    
    // 右边按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:[ColorUtil colorWithHexadecimal:@"#000000"] forState:UIControlStateNormal];
    btn.titleLabel.font = FONT(RATIO(28));
    [btn addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    self.rightBtn = btn;
    
    //默认左侧按钮
    self.titleType = MM_TitleTypeNone;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //添加约束
    [self.backBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.size.equalTo(CGSizeMake(44, 44-0.5));
        make.bottom.equalTo(self).offset(-RATIO(8));
    }];
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.greaterThanOrEqualTo(self.backBtn.right).offset(44);
//        make.bottom.equalTo(self).offset(-RATIO(12));
        make.centerY.equalTo(self.backBtn);
        make.centerX.equalTo(self);
    }];
    
    CGFloat w = 44;
    if (!stringIsEmpty(self.rightBtn.titleLabel.text)) {
        w = [self.rightBtn.titleLabel sizeThatFits:CGSizeMake(MAXFLOAT, 44-0.5)].width;
    }
    [self.rightBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-RATIO(20));
        make.centerY.equalTo(self.backBtn);
        make.size.equalTo(CGSizeMake(w, 44-0.5));
    }];
    
    if (self.isHidebtnBack)
    {
        self.backBtn.hidden = YES;
    }
}

#pragma mark - 设置
/**
 *  设置title样式
 *  设置约束 去除不需要的
 *  @param titleType 主要是右边的按钮
 */
-(void)setTitleType:(MM_TitleType)titleType
{
    _titleType = titleType;
    
    switch (titleType)
    {
        case MM_TitleTypeNone:
        {
            self.backBtn.hidden = NO;
            self.rightBtn.hidden = YES;
        }
            break;
        case MM_TitleTypeRight:
        {
            
            self.backBtn.hidden = NO;
            self.rightBtn.hidden = NO;
        }
            break;
        case MM_TitleTypeCustomSet:
        {
            self.backBtn.hidden = YES;
            self.rightBtn.hidden = YES;
        }
        default:
            break;
    }
}

-(void)setIsHidebtnBack:(BOOL)isHidebtnBack
{
    _isHidebtnBack = isHidebtnBack;
}

// 更改title名称
-(void)setTitle:(NSString *)title
{
    _title = [title copy];
    self.titleLabel.text = title;
}


#pragma mark - 事件

-(void)returnClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(MM_TitleView:clickBackButton:)])
    {
        [self.delegate MM_TitleView:self clickBackButton:sender];
    }
}

- (void)rightClick:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(MM_TitleView:clickRightButton:)]) {
        [self.delegate MM_TitleView:self clickRightButton:sender];
    }
}


@end
